<?php

namespace app\adminbackstage\service;

use app\adminbackstage\model\Address;
use app\adminbackstage\model\User;
use app\Response;

class MerchantService
{
    /***
     * 商户列表
    */
    public static function merchantListService($page,$num,$phone,$wx_account,$nickname,$username)
    {
        $where[] = ["type", "=", 20];
        if (!empty($phone)) {
            $where[] = ["phone", "like", "%" . $phone . "%"];
        }
        if (!empty($wx_account)) {
            $where[] = ["wx", "like", "%" . $wx_account . "%"];
        }
        if (!empty($nickname)) {
            $where[] = ["nickname", "like", "%" . $nickname . "%"];
        }
        if(!empty($username)){
            $where[] = ["username", "like", "%" . $username . "%"];
        }
        $list = User::datalist($where, $page, $num);
        if (!empty($list)) {
            $data = $list->toArray();
            foreach ($data as $k => $v) {
                $data[$k]['addtime'] = !empty($v['addtime']) ? date("Y-m-d H:i:s", $v['addtime']) : "";
                $data[$k]['lasttime'] = !empty($v['lasttime']) ? date("Y-m-d H:i:s", $v['lasttime']) : "";
            }
            $count = User::listCount($where);
        }else{
            $count = 0;
            $data = [];
        }
        return Response::wrapData($data,$count);
    }

    public static function merchantInfoService($m_id){
        $info = User::info(["uid"=>$m_id]);
        $info["addtime"] = !empty($info["addtime"])?date("Y-m-d H:i:s",$info["addtime"]):"";
        $info["lasttime"] = !empty($info["lasttime"])?date("Y-m-d H:i:s",$info["lasttime"]):"";
        $info["admin_link_up_time"] = !empty($info["admin_link_up_time"])?date("Y-m-d H:i:s",$info["admin_link_up_time"]):"";
        $info["admin_salesman_time"] = !empty($info["admin_salesman_time"])?date("Y-m-d H:i:s",$info["admin_salesman_time"]):"";
        return Response::wrapData($info);
    }


    /*** 添加商家
    */
    public static function merchantAddService($username,$password,$paid,$phone,$wx_account,$nickname,$remarks,$enable,$id){
        if(empty($id)){
            $userInfo = User::info(["phone"=>$phone]);
            if($userInfo){
                return Response::wrapError(0,"当前手机号已注册!");
            }
            $siteInfo = User::info(["uid"=>$paid]);
            if(empty($siteInfo)){
                return Response::wrapError(0,"站点信息异常!");
            }
            $encryption = AdminLoginService::createNonceStr( 4);
            $password = AdminLoginService::encPassword($password,$encryption);//密码
            $data = [
                "type" => 20,
                'paid' => $paid,
                "password" => $password,
                "encryption" => $encryption,
                "addtime" => time(),
                "username" => !empty($username)?$username:$phone,
                "nickname" => !empty($nickname)?$nickname:$phone,
                "phone" => $phone,
                "remarks" => $remarks,
                "wx" => $wx_account,
                "channel" => "总后台添加",
                "superor" => $siteInfo["nickname"]
            ];
            $res = User::userAdd($data);
        }else{
            $userInfo = User::info(["uid"=>$id]);
            if(!$userInfo){
                return Response::wrapError(0,"当前用户不存在!");
            }
            $data = [];
            if(!empty($username)&&empty($userInfo["username"])){
                $data["username"] = $username;
            }
            if(!empty($password)){
                $encryption = AdminLoginService::createNonceStr( 4);
                $password = AdminLoginService::encPassword($password,$encryption);//密码
                $data["password"] = $password;
                $data["encryption"] = $encryption;
            }
            if(!empty($wx_account)){
                $data["wx"] = $wx_account;
            }
            if(!empty($paid)){
                $data["paid"] = $paid;
            }
            if(!empty($nickname)) {
                $data["nickname"] = $nickname;
            }
            if (!empty($remarks)){
                $data["remarks"] = $remarks;
            }
            if(is_numeric($enable)){
                $data["enable"] = $enable;
            }
            $res = User::userEdit($id, $data);
        }

        if ($res) {
            return Response::wrapData();
        } else {
            return Response::wrapError(0, "操作失败！");
        }
    }

    /*** 商家地址列表查询
    */
    public static function merchantAddListService($m_id,$page,$num){
        if(empty($m_id)){
            return Response::wrapError(0,"参数异常！");
        }
        $where[] = ["uid","=",$m_id];
        $where[] = ["enable","=",1];
        $list = Address::datalist($where,$page,$num);
        if(!empty($list)){
            $data = $list->toArray();
            foreach ($data as $k => $v){
                $data[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
            $count = Address::listCount($where);
        }else{
            $data = [];
            $count = 0;
        }
        return Response::wrapData($data,$count);
    }

    /***
     * 添加商家地址
    */
    public static function merchantAddCreatService($m_id,$id,$data){
        if(!empty($id)){
            if($data["default"] == 1){
                $where_one[] = ["uid","=",$m_id];
                Address::addressEdit($where_one,["default"=>0]);
            }
            $where[] = ["id","=",$id];
            $res = Address::addressEdit($where,$data);
        }else{
            if($data["default"] == 1){
                $where_one[] = ["uid","=",$m_id];
                Address::addressEdit($where_one,["default"=>0]);
            }
            $res = Address::addressInsert($data);
        }
        return $res;
    }

    /*** 商家地址详情
    */
    public static function merchantAddDetailService($id){
        if(empty($id)){
            return Response::wrapError(0,"参数异常！");
        }
        $where[] = ["id","=",$id];
        $res = Address::addressInfo($where);
        return Response::wrapData($res);
    }

    /*** 商家地址删除
    */
    public static function merchantAddDeleteService($id){
        if(empty($id)){
            return Response::wrapError(0,"参数异常！");
        }
        $where[] = ["id","=",$id];
        $res = Address::addressEdit($where,["enable"=>0]);
        if ($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"操作失败!");
        }
    }
}